<template>
	<view class="u-page">
		<u-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	var calanderURL = "content://com.android.calendar/calendars";
	var calanderEventURL = "content://com.android.calendar/events";
	var calanderRemiderURL = "content://com.android.calendar/reminders";
	var calId;
	export default {
		methods: {
			submit() {
				uni.getSystemInfo({
					success: res => {
						if (res.platform === 'android') {
							this.addEvent({
								title: '测试',
								description: '测试内容',
								dtstart: new Date().getTime(),
								dtend: new Date().getTime() + 1000 * 3600,
							})
						} else {
							// #ifdef APP-PLUS
							this.$refs.uToast.show({
								type: 'error',
								message: "不是安卓",
							})
							// #endif
						}
					}
				})
			},
			addEvent(data) {
				var Cursor = plus.android.importClass("android.database.Cursor");
				var Uri = plus.android.importClass("android.net.Uri");
				var Calendar = plus.android.importClass("java.util.Calendar");
				var main = plus.android.runtimeMainActivity();
				var userCursor = plus.android.invoke(main.getContentResolver(), "query", Uri.parse(calanderURL), null,
					null, null, null);
				//判断是否有账户
				if (plus.android.invoke(userCursor, "getCount") <= 0) {
					this.initCalendars();
				} else {
					plus.android.invoke(userCursor, "moveToLast");
					calId = plus.android.invoke(userCursor, "getString", plus.android.invoke(userCursor, "getColumnIndex",
						"_id"));
					var ContentValues = plus.android.importClass("android.content.ContentValues");
					var events = new ContentValues();
					events.put("title", data.title);
					events.put("description", data.description);
					// 插入账户  
					events.put("calendar_id", calId);
					console.log(calId);
					//位置
					// events.put("eventLocation", "位置");
					events.put("dtstart", data.dtstart); //时间戳 
					events.put("dtend", data.dtend); //时间戳 
					events.put("hasAlarm", 1);
					events.put("eventTimezone", "Asia/Shanghai"); // 这个是时区，必须有，  
					// 添加事件    
					var newEvent = plus.android.invoke(plus.android.runtimeMainActivity().getContentResolver(), "insert",
						Uri.parse(calanderEventURL), events);
					// 事件提醒的设定  
					var id = plus.android.invoke(newEvent, "getLastPathSegment");
					var values = new ContentValues();
					values.put("event_id", id);
					// 提前30分钟有提醒  
					values.put("minutes", "30");
					values.put("method", "1");
					plus.android.invoke(main.getContentResolver(), "insert", Uri.parse(calanderRemiderURL), values);
				}
			},
			//添加账户          
			initCalendars() {
				var TimeZone = plus.android.importClass("java.util.TimeZone");
				console.log(TimeZone)
				var timeZone = TimeZone.getDefault();
				console.log(timeZone)
				var ContentValues = plus.android.importClass("android.content.ContentValues");
				console.log(ContentValues)
				var value = new ContentValues();
				var Calendars = plus.android.importClass("android.provider.CalendarContract.Calendars");
				console.log(Calendars)
				value.put("name", "名称");
				// 账户名称
				value.put("account_name", "账户名称");
				// 账户类型
				value.put("account_type", "com.android.exchange");
				// 显示的账户名称
				value.put("calendar_displayName", "显示的账户名称");
				value.put("visible", 1);
				value.put("calendar_color", -9206951);
				value.put("calendar_access_level", "700");
				value.put("sync_events", 1);
				value.put("calendar_timezone", plus.android.invoke(timeZone, "getID"));
				// 账户拥有者
				value.put("ownerAccount", "账户拥有者");
				value.put("canOrganizerRespond", 0);
				console.log(value)
				var Uri = plus.android.importClass("android.net.Uri");
				var calendarUri = Uri.parse("content://com.android.calendar/calendars");
				var buildUpon = plus.android.invoke(calendarUri, "buildUpon");
				var CalendarContract = plus.android.importClass("android.provider.CalendarContract");
				plus.android.invoke(buildUpon, "appendQueryParameter", CalendarContract.CALLER_IS_SYNCADAPTER, "true");
				plus.android.invoke(buildUpon, "appendQueryParameter", "account_name", "账户名称");
				plus.android.invoke(buildUpon, "appendQueryParameter", "account_type", "com.android.exchange");
				calendarUri = plus.android.invoke(buildUpon, "build");
				plus.android.invoke(plus.android.runtimeMainActivity().getContentResolver(), "insert", calendarUri, value);

			}
		}
	}
</script>
<style lang="scss">
	.u-page {
		padding: 30rpx;
	}
</style>
